package com.generator.statics.outboundStatics.controller;

import com.generator.statics.outboundStatics.pojo.*;
import com.generator.statics.outboundStatics.service.impl.OutCountService;
import com.generator.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/OutCount")
public class OutCountAction {

    @Autowired
    private OutCountService outCountService;

    public OutCountService getOutCountService() {
        return outCountService;
    }

    public void setOutCountService(OutCountService outCountService) {
        this.outCountService = outCountService;
    }

    //总库存页面
    @ResponseBody
    @RequestMapping("/TotalHouseList")
    public PageModel<OutCountObject> TotalHouseList(OutCountObject warehouse,HttpServletRequest req){
        PageModel<OutCountObject> outCountObjectPageModel = new PageModel<>();
        String pagenum = req.getParameter("pagenum");
        if(pagenum!=null){
            Integer valueOf = Integer.valueOf(pagenum);
            outCountObjectPageModel.setPageNum(valueOf);
        }
        PageModel<OutCountObject> userListByCondition = outCountService.getTotalOutCount(warehouse, outCountObjectPageModel);
        if (userListByCondition.getData()!=null){
            if (!userListByCondition.getData().isEmpty()){
                return userListByCondition;
            }
        }
        return null;
    }

    //某一个仓库的出库详情页
    @ResponseBody
    @RequestMapping("/OutCountDetail")
    public PageModel<OutCountDetail> OutCountDetail(OutCountDetail warehouse,HttpServletRequest req,HttpSession session){
        warehouse.setHousename((String)session.getAttribute("housename"));
        PageModel<OutCountDetail> outCountObjectPageModel = new PageModel<>();
        String pagenum = req.getParameter("pagenum");
        if(pagenum!=null){
            Integer valueOf = Integer.valueOf(pagenum);
            outCountObjectPageModel.setPageNum(valueOf);
        }
        PageModel<OutCountDetail> outCountDetail = outCountService.getOutCountDetail(warehouse, outCountObjectPageModel);
        if (outCountDetail.getData()!=null){
            if (!outCountDetail.getData().isEmpty()){
                return outCountDetail;
            }
        }
        return null;
    }

    //去到详情页并传入库存名字到session
    @ResponseBody
    @RequestMapping("/toDeliveryView")
    public void toDeliveryView(HttpServletRequest req,HttpSession session){
        String housename = req.getParameter("housename");
        session.setAttribute("housename", housename);
    }

    //去到订单详情页并传入订单编号到session
    @ResponseBody
    @RequestMapping("/toCommentDeliveryView")
    public void toCommentDeliveryView(HttpServletRequest req,HttpSession session){
        String orderid = req.getParameter("orderid");
        session.setAttribute("orderid", orderid);
    }

    //取得订单页的详情（不包含商品信息）
    @ResponseBody
    @RequestMapping("/getDelivaeryDetail")
    public OutCountDelivery getDelivaeryDetail(HttpSession session){
        String orderid = (String)session.getAttribute("orderid");
        OutCountDelivery outCountDelivery = new OutCountDelivery();
        outCountDelivery.setOrderid(orderid);
        OutCountDelivery deliveryByOrderId = outCountService.getDeliveryByOrderId(outCountDelivery);
        return deliveryByOrderId;
    }

    //取得订单页含商品信息
    @ResponseBody
    @RequestMapping("/getCommentDetialByOrderId")
    public List<CommentDetial> getCommentDetialByOrderId(HttpSession session){
        String orderid = (String)session.getAttribute("orderid");
        CommentDetial commentDetial = new CommentDetial();
        commentDetial.setOrderid(orderid);
        List<CommentDetial> commentDetialByOrderId = outCountService.getCommentDetialByOrderId(commentDetial);
        return commentDetialByOrderId;
    }

    //搜索功能--省份
    @ResponseBody
    @RequestMapping("/getProvince")
    public List<Province> getProvince(HttpSession session){
        Province province = new Province();
        List<Province> allProvince = outCountService.getAllProvince(province);
        return  allProvince;
    }

    //搜索功能--城市
    @ResponseBody
    @RequestMapping("/cheakprovince")
    public List<City> cheakprovince(HttpSession session,HttpServletRequest req){
        String province = req.getParameter("province");
        City city = new City();
        city.setParentname(province);
        List<City> allCity = outCountService.getAllCity(city);
        return  allCity;
    }

    //搜索功能--完整
    @ResponseBody
    @RequestMapping("/checkStatic")
    public PageModel<OutCountObject> checkStatic(HttpSession session,HttpServletRequest req,PageModel<OutCountObject> pageModel){
        String checkhouse = req.getParameter("checkhouse");
        String checkaddress = req.getParameter("checkaddress");
        String checkaddresscity = req.getParameter("checkaddresscity");
        OutCountObject outCountObject = new OutCountObject();
        if(checkhouse==""&checkaddress!=""&checkaddresscity==""){
            outCountObject.setHouseaddress(checkaddress);
        }else if(checkhouse==""&checkaddress!=""&checkaddresscity!=""){
            outCountObject.setHouseaddress(checkaddress);
            outCountObject.setHouseaddresscity(checkaddresscity);
        }else if(checkhouse!=""&checkaddress==""){
            outCountObject.setHousename(checkhouse);
        }else if(checkhouse!=""&checkaddress!=""&checkaddresscity==""){
            outCountObject.setHousename(checkhouse);
            outCountObject.setHouseaddress(checkaddress);
        }else if(checkhouse!=""&checkaddress!=""&checkaddresscity!=""){
            outCountObject.setHousename(checkhouse);
            outCountObject.setHouseaddress(checkaddress);
            outCountObject.setHouseaddresscity(checkaddresscity);
        }
        PageModel<OutCountObject> totalOutCount = outCountService.getTotalOutCount(outCountObject, pageModel);
        return  totalOutCount;

    }
}
